package com.taxi.driver.module.order.setting;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.taxi.driver.BuildConfig;
import com.taxi.driver.R;
import com.taxi.driver.common.BaseFragment;
import com.taxi.driver.config.DriverType;
import com.taxi.driver.config.RemindType;
import com.taxi.driver.module.order.setting.dagger.DaggerOrderSettingComponent;
import com.taxi.driver.module.order.setting.dagger.OrderSettingModule;
import com.taxi.driver.util.SpeechUtil;
import com.taxi.driver.widget.select.SelectView;
import com.taxi.driver.widget.slide.SlideSwitch;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 功能描述：听单设置
 */
public class OrderSettingFragment extends BaseFragment implements OrderSettingContract.View, SelectView.ActionListener {

    public static OrderSettingFragment newInstance() {
        return new OrderSettingFragment();
    }

    @Inject
    OrderSettingPresenter mPresenter;

    @BindView(R.id.ss_type)
    SlideSwitch mSsType;
    @BindView(R.id.ss_config)
    SlideSwitch mSsConfig;
    @BindView(R.id.ss_config2)
    SlideSwitch mSsConfig2;

    @BindView(R.id.tv_tag)
    TextView mTvTag;
    @BindView(R.id.layout_tag)
    LinearLayout mLayoutTag;

    @BindView(R.id.sv_address)
    SelectView mSvAddress;
    @BindView(R.id.sv_start)
    SelectView mSvStart;
    @BindView(R.id.sv_end)
    SelectView mSvEnd;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_order_setting, container, false);
        ButterKnife.bind(this, mView);
        mPresenter.onCreate();

        if ((BuildConfig.FLAVOR_project.equals("Q201705XuanXuan"))
                && (mPresenter.driverType() == DriverType.SPECIAL)) {
            mSsConfig.setVisibility(View.GONE);
            mSsConfig2.setVisibility(View.VISIBLE);
            mSsConfig2.setOnChangeListener(position -> { //监听听单偏好的变化
                int remindType = RemindType.ALL; //听单偏好
                switch (position) {
                    case 0: //实时
                        remindType = RemindType.REALTIME;
                        break;
                    case 1: //预约
                        remindType = RemindType.APPOINT;
                        break;
                    case 2: //日租/半日租（包车）
                        remindType = RemindType.RENT;
                        break;
                    default: //全部
                        remindType = RemindType.ALL;
                        break;
                }
                mPresenter.selectRemindType(remindType); //改变偏好设置
            });
        } else {
            mSsConfig.setVisibility(View.VISIBLE);
            mSsConfig2.setVisibility(View.GONE);
            mSsConfig.setOnChangeListener(position -> { //监听听单偏好的变化
                int remindType = RemindType.ALL; //听单偏好
                switch (position) {
                    case 0: //实时
                        remindType = RemindType.REALTIME;
                        break;
                    case 1: //预约
                        remindType = RemindType.APPOINT;
                        break;
                    default: //全部
                        remindType = RemindType.ALL;
                        break;
                }
                mPresenter.selectRemindType(remindType); //改变偏好设置
            });
        }

        mSvStart.setOnActionListener(this);
        mSvEnd.setOnActionListener(this);
        return mView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        DaggerOrderSettingComponent.builder()
                .appComponent(getAppComponent())
                .orderSettingModule(new OrderSettingModule(this))
                .build().inject(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.subscribe();
    }

    @Override
    public void onPause() {
        super.onPause();
        mPresenter.unsubscribe();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mPresenter.onDestory();
    }

    @Override
    public void onClick(SelectView view) {
        switch (view.getId()) {
            case R.id.sv_start:
                mPresenter.selectStart();
                break;
            case R.id.sv_end:
                mPresenter.selectEnd();
                break;
        }
    }

    @Override
    public void onDelete(SelectView view) {
        switch (view.getId()) {
            case R.id.sv_start:
                mPresenter.clearStart();
                break;
            case R.id.sv_end:
                mPresenter.clearEnd();
                break;
        }
    }

    @Override
    public void changeStart(String start) {
        mSvStart.setText(start);
    }

    @Override
    public void changeEnd(String end) {
        mSvEnd.setText(end);
    }

    @Override
    public void setSsConfigPosition(int position) {
        if (( BuildConfig.FLAVOR_project.equals("Q201705XuanXuan"))
                && (mPresenter.driverType() == DriverType.SPECIAL )) {
            mSsConfig2.setPosition(position);
        } else {
            mSsConfig.setPosition(position);
        }
    }

    /**
     * 控制预约时段的显示或隐藏
     *
     * @param remindType
     */
    @Override
    public void setAppointTimeDisplay(int remindType) {
        //如果是"实时"，隐藏"预约时间段"
        int vis = (remindType == RemindType.REALTIME)
                ? View.GONE : View.VISIBLE;
        mTvTag.setVisibility(vis);
        mLayoutTag.setVisibility(vis);
    }

    @Override
    public void saveRemindTypeSuccess() {
        SpeechUtil.speech(getContext(), "完成设置");
        getActivity().finish();
    }

    @OnClick(R.id.tv_complete)
    public void onClick() {
        mPresenter.reqSaveRemindType();
    }

}
